package com.yilin.platform.controller;


import com.yilin.platform.base_core.application.BaseController;
import com.yilin.platform.base_core.application.ReturnData;
import com.yilin.platform.base_core.commons.exception.BusinessException;
import com.yilin.platform.base_core.commons.utils.ParamUtil;
import com.yilin.platform.base_core.interceptor.EncryptInterceptor;
import com.yilin.platform.base_core.interceptor.NoBindingFilter;
import com.yilin.platform.base_core.interceptor.SkipLoginVerify;
import com.yilin.platform.entity.aliPay.AliPayTradeRecord;
import com.yilin.platform.service.IAliPayService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/platform/alipay/")
public class AliPayController extends BaseController {

    @Resource
    private IAliPayService aliPayService;
    /**
     * 支付宝下单
     * */
    @PostMapping(value = "tradePaymentOrder")
    @EncryptInterceptor
    public ReturnData<Object> tradePaymentOrder(AliPayTradeRecord aliPayTradeRecord,String trminalType,String requestFromURl) {
        ParamUtil.isNull(aliPayTradeRecord.getTradeNo(), "本地流水号为空！");
        ParamUtil.isNull(aliPayTradeRecord.getTradeName(), "交易名称为空！");
        ParamUtil.isNull(aliPayTradeRecord.getOutOrganizeId(), "付款组织为空！");
        ParamUtil.isNull(aliPayTradeRecord.getOutAcctName(), "付款人名称为空！");
        ParamUtil.isNull(aliPayTradeRecord.getInOrganizeId(), "收款人组织为空！");
        ParamUtil.isNull(aliPayTradeRecord.getInAcctName(), "收款人名称为空！");
        ParamUtil.isNull(aliPayTradeRecord.getTranAmount(), "交易金额为空！");
        ParamUtil.isNull(aliPayTradeRecord.getUserType(), "用户类型为空！");
        String object=null;
        AliPayTradeRecord aliPayTradeRecordOld=aliPayService.getAliPayTradeRecordByTradeNo(aliPayTradeRecord.getTradeNo());
        if(aliPayTradeRecordOld!=null){
           if(aliPayTradeRecord.getAliPayTradeStatus()==AliPayTradeRecord.AliPayTradeStatus.handle){
                throw new BusinessException("平台：该订单号正在处理中请稍后重试：");
            }else if(aliPayTradeRecord.getAliPayTradeStatus()==AliPayTradeRecord.AliPayTradeStatus.success){
                throw new BusinessException("平台：该订单单号以存在");
            }
            aliPayTradeRecord.setId(aliPayTradeRecordOld.getId());
        }
        if(trminalType.equals("app")){
            object=aliPayService.generatorAliPayOrder(aliPayTradeRecord);
        }else if(trminalType.equals("web")){
            object=aliPayService.generatorAliPayOrderPcH5(aliPayTradeRecord,requestFromURl);
        }else{
            return jsonView(false, "识别设备类型错误", trminalType);
        }
        return jsonView(SUCCESS_CODE, "下单成功", object);
    }
    /**
     * 支付宝下单查询
     * */
    @PostMapping(value = "queryAliPayOrder")
    @EncryptInterceptor
    public ReturnData<AliPayTradeRecord> queryAliPayOrder(String tradeNo) {
        ParamUtil.isNull(tradeNo, "流水号为空！");
        AliPayTradeRecord body= aliPayService.queryAliPayOrder(tradeNo);
        return jsonView(SUCCESS_CODE, "查询成功", body);
    }
    /**
     * 支付退款
     * */
    @PostMapping(value = "aliPayRefund")
    @EncryptInterceptor
    public ReturnData<AliPayTradeRecord> aliPayRefund(AliPayTradeRecord aliPayTradeRecord) {
        ParamUtil.isNull(aliPayTradeRecord.getTradeNo(), "本地流水号为空！");
        ParamUtil.isNull(aliPayTradeRecord.getTradeName(), "交易名称为空！");
        ParamUtil.isNull(aliPayTradeRecord.getOutOrganizeId(), "付款组织为空！");
        ParamUtil.isNull(aliPayTradeRecord.getOutAcctName(), "付款人名称为空！");
        ParamUtil.isNull(aliPayTradeRecord.getInOrganizeId(), "收款人组织为空！");
        ParamUtil.isNull(aliPayTradeRecord.getInAcctName(), "收款人名称为空！");
        ParamUtil.isNull(aliPayTradeRecord.getRefundOriginalTradeNo(), "原交易订单号为空！");
        AliPayTradeRecord aliPayTradeRecordOrging=aliPayService.getAliPayTradeRecordByTradeNo(aliPayTradeRecord.getRefundOriginalTradeNo());
        ParamUtil.isNull(aliPayTradeRecordOrging, "原交易订单为空！");
        if(aliPayTradeRecordOrging.getAliPayTradeStatus()== AliPayTradeRecord.AliPayTradeStatus.handle){
            aliPayTradeRecordOrging=aliPayService.queryAliPayOrder(aliPayTradeRecordOrging.getTradeNo());
        }
        ParamUtil.notEquals("交易单未支付成功，退款失败",aliPayTradeRecordOrging.getAliPayTradeStatus(),AliPayTradeRecord.AliPayTradeStatus.success);
        ParamUtil.notEquals("原付款人和收款人不是同一个，退款失败",aliPayTradeRecord.getInOrganizeId(),aliPayTradeRecordOrging.getOutOrganizeId());
        AliPayTradeRecord body= aliPayService.aliPayRefund(aliPayTradeRecord,aliPayTradeRecordOrging);
        return jsonView(SUCCESS_CODE, "退款成功", body);
    }

    /**
     * 支付宝退款查询
     * */
    @PostMapping(value = "aliPayRefundQuery")
    @EncryptInterceptor
    public ReturnData<AliPayTradeRecord> aliPayRefundQuery(String tradeNo) {
        ParamUtil.isNull(tradeNo, "流水号为空！");
        AliPayTradeRecord body= aliPayService.aliPayRefundQuery(tradeNo);
        return jsonView(SUCCESS_CODE, "查询成功", body);
    }

    /**
     * 支付宝支付回调
     */
    @PostMapping(value = "aliPayBack/{userType}")
    @SkipLoginVerify
    @NoBindingFilter
    public String aliPayBack( @PathVariable("userType") String userType, HttpServletRequest request, HttpServletResponse response) {
        Map<String,Object> map=aliPayService.aliPayNotity(request,userType,"payment");
        return String.valueOf(map.get("status"));
    }
    /**
     * 支付宝支付退款回调 支付宝退款回调已弃用  支付宝平台不会返回
     */
    @PostMapping(value = "aliRefundBack/{userType}")
    @SkipLoginVerify
    @NoBindingFilter
    public String aliRefundBack( @PathVariable("userType") String userType, HttpServletRequest request, HttpServletResponse response) {
        Map<String,Object> map=aliPayService.aliPayNotity(request,userType,"refund");
        return String.valueOf(map.get("status"));
    }

}
